# jQuery offset() Method

jQuery HTML/CSS Methods

## Example

Return the offset coordinates of a <p> element:

$("button").click(function(){
var x=$("p").offset();
alert("Top: " + x.top + " Left: " + x.left);
});
Try it yourself »

## Definition and Usage

The offset() method set or returns the offset coordinates for the selected elements, relative to the document.

When used to return the offset:
This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.

When used to set the offset:
This method sets the offset coordinates of ALL matched elements.

## Syntax

Return the offset coordinates:

$(selector).offset()

Set the offset coordinates:

$(selector).offset({top:value,left:value})

Set offset coordinates using a function:

$(selector).offset(function(index,currentoffset))

Parameter | Description
--- | ---

{top:value,left:value}

Required when setting the offset. Specifies the top and left coordinates in pixels.
Possible values:

- Name/Value pairs, like {top:100,left:100}
- An object with top and left properties (example)

function(index,currentoffset)

Optional. Specifies a function that returns an object containing the top and left coordinates

- index - Returns the index position of the element in the set
- currentoffset - Returns the current coordinates of the selected element

## Try it Yourself - Examples

Set the offset coordinates
How to set the offset coordinates of an element.

Set offset coordinates using a function
Using a function to set the offset coordinates of an element.

Set the offset coordinates for an element using an object
How to set the offset coordinates for an element using a new object.

Set the offset coordinates for an element using the offset coordinates of another element
How to set the offset coords for an element using the offset coords of an existing element.

jQuery HTML/CSS Methods

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples